<%--
Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--%>

<%@ Control Language="C#" ClassName="RatingControl" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>

<%@ Import Namespace="Grr.Web" %>
<%@ Import Namespace="Grr.Model" %>

<script runat="server">
   
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!IsPostBack)
        {
            Grurrah g = (Grurrah) Session["CurrentGrurrah"];
            if (g == null)
                return;

            RatingAverageHiddenField.Value = g.Rating.ToString ();
            switch (g.Rating)
            {
                case 5:
                    ImageButton5.ImageUrl = "~/media/rating-star-full.gif";
                    goto case 4;
                case 4:
                    ImageButton4.ImageUrl = "~/media/rating-star-full.gif";
                    goto case 3;
                case 3:
                    ImageButton3.ImageUrl = "~/media/rating-star-full.gif";
                    goto case 2;
                case 2:
                    ImageButton2.ImageUrl = "~/media/rating-star-full.gif";
                    goto case 1;
                case 1:
                    ImageButton1.ImageUrl = "~/media/rating-star-full.gif"; 
                    break;
                default:
                    break;
            }
        }
    }

    private void SubmitRating(int rating)
    {
        GrurrahUser user = Session["CurrentUser"] as GrurrahUser;

        if (user != null)
        {
            Grurrah g = (Grurrah)Session["CurrentGrurrah"];
            if (g == null)
            {
                RaiseBubbleEvent(this, new UserNotificationEventArgs("Sorry, you can't rate your Grurrah until after it has been created."));
            }
            else
            {
                g.SubmitRating(user, rating);
                ClearRatingButton.Visible = true;
                RatingLockedHiddenField.Value = "true";
            }
        } else {
            RaiseBubbleEvent(this, new UserNotificationEventArgs("Sorry, you must be logged in to rate Grurrahs."));
        }
    }

    private void ClearRating()
    {
        GrurrahUser user = Session["CurrentUser"] as GrurrahUser;

        if (user != null)
        {
            Grurrah g = (Grurrah)Session["CurrentGrurrah"];
            if (g == null)
            {
                RaiseBubbleEvent(this, new UserNotificationEventArgs("Sorry, you can't rate your Grurrah until after it has been created."));
            }
            else
            {
                g.ClearRating(user);
                ClearRatingButton.Visible = false;
                RatingLockedHiddenField.Value = "false";
            }
        }
        else
        {
            RaiseBubbleEvent (this, new UserNotificationEventArgs ("Sorry, you must be logged in to rate Grurrahs."));
        }
    }
    
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SubmitRating(1);   
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        SubmitRating(2);
    }

    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        SubmitRating(3);
    }

    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        SubmitRating(4);
    }

    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        SubmitRating(5);
    }

    protected void ClearRatingButton_Click(object sender, ImageClickEventArgs e)
    {
        ClearRating();
    }
</script>


<gaia:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/media/rating-star-empty.gif" AlternateText="Rate this a one" CssClass="RatingsStar" onmouseout="ratings_clear ();" onmouseover="ratings_set (1);" OnClick="ImageButton1_Click" /><gaia:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/media/rating-star-empty.gif" AlternateText="Rate this a two" CssClass="RatingsStar" onmouseout="ratings_clear ();" onmouseover="ratings_set (2);" OnClick="ImageButton2_Click" /><gaia:ImageButton ID="ImageButton3" runat="server" AlternateText="Rate this a three" ImageUrl="~/media/rating-star-empty.gif" CssClass="RatingsStar" onmouseout="ratings_clear ();" onmouseover="ratings_set (3);" OnClick="ImageButton3_Click" /><gaia:ImageButton ID="ImageButton4" runat="server" AlternateText="Rate this a four" ImageUrl="~/media/rating-star-empty.gif" CssClass="RatingsStar" onmouseout="ratings_clear ();" onmouseover="ratings_set (4);" OnClick="ImageButton4_Click" /><gaia:ImageButton ID="ImageButton5" runat="server" AlternateText="Rate this a five" ImageUrl="~/media/rating-star-empty.gif" CssClass="RatingsStar" onmouseout="ratings_clear ();" onmouseover="ratings_set (5);" OnClick="ImageButton5_Click" />
<gaia:ImageButton ID="ClearRatingButton" runat="server" AlternateText="Clear your rating" ImageUrl="../media/rating-clear.gif" Visible="false" OnClick="ClearRatingButton_Click" />

<div id="rating_avg_container"><gaia:HiddenField ID="RatingAverageHiddenField" runat="server" /></div>
<div id="rating_locked_container"><gaia:HiddenField ID="RatingLockedHiddenField" runat="server" /></div>
